package com.lagou.ipersistence.reflection.property;

import java.util.Iterator;

/**
 * @author Yan Jiahong
 * Created on 2020/11/22
 */
public class PropertyTokenizer implements Iterator<PropertyTokenizer> {
    private String name;
    private final String indexName;
    private String index;
    private final String children;

    public PropertyTokenizer(String fullName) {
        int delimiterIndex = fullName.indexOf(".");
        if (delimiterIndex > -1) {
            name = fullName.substring(0, delimiterIndex);
            children = fullName.substring(delimiterIndex + 1);
        } else {
            name = fullName;
            children = null;
        }
        indexName = name;
        delimiterIndex = name.indexOf(".");
        if (delimiterIndex > -1) {
            index = name.substring(delimiterIndex + 1, name.length() - 1);
            name = name.substring(0, delimiterIndex);
        }
    }


    @Override
    public boolean hasNext() {
        return children != null;
    }

    @Override
    public PropertyTokenizer next() {
        return new PropertyTokenizer(children);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("不支持删除操作, 因为它在property中是无意义的!");
    }

    public String getName() {
        return name;
    }

    public String getIndexName() {
        return indexName;
    }

    public String getIndex() {
        return index;
    }

    public String getChildren() {
        return children;
    }
}
